//
//  LWUtilityClass.m
//  LWShopItem
//
//  Created by 高子雄 on 2017/5/3.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWUtilityClass.h"
#import <CommonCrypto/CommonCrypto.h>
#import "LWLogInVc.h"

@implementation LWUtilityClass

+ (NSMutableDictionary *)isDict:(NSMutableDictionary *)dict
{
    NSString *str = [self ascendingString:dict];
    NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    dict[@"unique_id"] = uuid;
    
    NSString *timae = [self isTimaeData];
    dict[@"time"] = timae; // 添加时间戳
    dict[@"sign"] = [[NSString stringWithFormat:@"%@%@%@shop.sdlinwang.com",str,uuid,timae] md5String]; // 添加sing
        
    return dict;
}
// 升序  然后返回排序后的字符串
+(NSString *)ascendingString:(NSMutableDictionary *)dict
{
    NSMutableArray *numArray = [NSMutableArray arrayWithArray:dict.allKeys];
    [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {  // 对数组里的参数进行排序
        return [str1 compare:str2];
    }];
    __block NSString *str = [NSString string];
    [numArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        str = [str stringByAppendingString:dict[obj]];
    }];
     return  str;
}
// 获取时间戳
+ (NSString *)isTimaeData
{
    NSTimeInterval a = [[NSDate date] timeIntervalSince1970] ;
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];  //转为字符型
    return timeString;
}
// 获取 token
+(NSString *)takeAToken
{
    NSString *user_token = [self UnArchiverAnObjectWithFileName:@"token"];
    if (user_token == nil)user_token = @"";
    return user_token;
}

/**
 *  根据名称 归档对象
 */
+(BOOL)ArchiverAnObjectWithFileName:(NSString*)fileName andAchiverObject:(id)archiverObj
{
    return  [NSKeyedArchiver archiveRootObject:archiverObj toFile:[self getAbsolutePathWithFileName:fileName]];
}
/**
 *  根据名称解档对象
 */
+(id)UnArchiverAnObjectWithFileName:(NSString*)fileName
{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[self getAbsolutePathWithFileName:fileName]];
}
/**
 *   根据传过来的文件名  拼接到cache文件夹路径后 此处只传入文件名
 */
+(NSString*)getAbsolutePathWithFileName:(NSString*)fileName
{
    NSString *cache=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath=[cache stringByAppendingPathComponent:fileName];
    filePath=[filePath stringByAppendingString:@".data"];
    return filePath;
}
// 跳转登录界面
+ (void) VC:(UIViewController *)VC
{
   // UIStoryboard *sb=[UIStoryboard storyboardWithName:@"LWLogInVc" bundle:nil];
    
    UIViewController *sb = [[UIViewController alloc]initWithNibName:@"LWLogInVc" bundle:[NSBundle mainBundle]];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:sb];
    
    [VC  presentViewController:nav animated:YES completion:nil];
    
}

@end
